<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="类型ID" prop="typeId">
        <el-input
            v-model="queryParams.typeId"
            placeholder="请输入类型ID"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="题目" prop="problemTitle">
        <el-input
            v-model="queryParams.problemTitle"
            placeholder="请输入题目"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="答题类型" prop="answerType">
        <el-select v-model="queryParams.answerType" placeholder="请选择答题类型" clearable>
          <el-option
              v-for="dict in train_question_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="难度" prop="problemDifficulty">
        <el-select v-model="queryParams.problemDifficulty" placeholder="请选择难度" clearable>
          <el-option
              v-for="dict in question_train_difficulty"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="trainProblemList" height="45vh" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="类型" align="center" min-width="160" prop="typeId">
        <template #default="scope">
          {{ getTypeById(scope.row.typeId) }}
        </template>
      </el-table-column>
      <el-table-column label="难度" align="center" min-width="80" prop="problemDifficulty">
        <template #default="scope">
          <dict-tag :options="question_train_difficulty" :value="scope.row.problemDifficulty"/>
        </template>
      </el-table-column>
      <el-table-column label="题目" align="center" min-width="180" prop="problemTitle"/>
      <el-table-column label="答题类型" align="center" min-width="100" show-overflow-tooltip prop="answerType">
        <template #default="scope">
          <dict-tag :options="train_question_type" :value="scope.row.answerType"/>
        </template>
      </el-table-column>
      <el-table-column label="选项1" align="center" min-width="180" show-overflow-tooltip prop="answerOne"/>
      <el-table-column label="选项2" align="center" min-width="180" show-overflow-tooltip prop="answerTwo"/>
      <el-table-column label="选项3" align="center" min-width="180" show-overflow-tooltip prop="answerThree"/>
      <el-table-column label="选项4" align="center" min-width="180" show-overflow-tooltip prop="answerFour"/>
      <el-table-column label="选项5" align="center" min-width="180" show-overflow-tooltip prop="answerFive"/>
      <el-table-column label="答案" align="center" min-width="180" show-overflow-tooltip prop="realAnswer"/>
      <el-table-column label="问题分数" align="center" prop="problemScore"/>
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
        style="position: relative !important;"
    />
  </div>
</template>

<script setup name="SelectProblem">
import {listTrainProblemExtrActivity} from "@/api/question/trainProblem";
import {listTrainProblemTypeAll} from "@/api/question/trainProblemType";

const {proxy} = getCurrentInstance();

const emit = defineEmits(['change']);

const props = defineProps({
  activityId: String
})

const {
  train_question_type,
  question_train_difficulty
} = proxy.useDict('train_question_type', 'question_train_difficulty');

const trainProblemList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    typeId: null,
    problemTitle: null,
    answerType: null,
    answerOne: null,
    answerTwo: null,
    answerThree: null,
    answerFour: null,
    answerFive: null,
    realAnswer: null,
    problemScore: null,
    problemDifficulty: null,
  },
  rules: {
    typeId: [{ required: true, message: "题目类型不能为空", trigger: "blur" },]
  }
});

const {queryParams, form, rules} = toRefs(data);

const typeList = ref([]);

const selectList = ref([]);

/** 查询答题题目列表 */
function getList() {
  loading.value = true;
  queryParams.value.activityId = props.activityId;
  console.log(queryParams.value, '2222')
  console.log(props.activityId, '333')
  listTrainProblemExtrActivity(queryParams.value).then(response => {
    trainProblemList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    typeId: null,
    problemTitle: null,
    answerType: 0,
    answerOne: null,
    answerTwo: null,
    answerThree: null,
    answerFour: null,
    answerFive: null,
    realAnswer: null,
    problemScore: null,
    problemDifficulty: null,
    createTime: null,
    createBy: null,
    updateTime: null,
    updateBy: null,
    delFlag: null
  };
  proxy.resetForm("trainProblemRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  selectList.value = selection;
  emit('change', selectList.value)
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加答题题目";
}

function getTypeList() {
  listTrainProblemTypeAll().then(res => {
    if (res.code === 200) {
      typeList.value = res.data;
    }
  })
}

function getTypeById(id) {
  if (typeList.value && typeList.value.length > 0) {
    const matArr = typeList.value.filter(x => x.id === id);
    if (matArr && matArr.length > 0) {
      return matArr[0].typeName
    }
  }
  return '-'
}

getList();
getTypeList();
</script>
